/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../tools/head.h"
#include "../impl/head.h"
#include "config.h"

namespace xos_http
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    static config * config_ptr = 0;

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    config::config()
    {
        // heart
        heart_check_interval_ms = 1000;

        // config obj
        m_pLastConfigProp = 0;
        m_pConfigProp = 0;
        m_pConfig = 0;

        // params
        db_connect_str = "DRIVER={MySQL ODBC 8.0 Unicode Driver};SERVER=127.0.0.1;PORT=3306;DATABASE=xoskit;UID=root;PWD=root;CharSet=utf8mb4;";
        m_listener = "listener";
        m_servlet = "servlet";
        m_before = "before";
        m_after = "after";

        // app path
        m_hot_replace_xos = "./";
        m_default_host = "default";
        m_web_root = "xos_http";
        m_filter = "filter";
        m_root = "root";
        m_ssl = "ssl";

        // file type
        m_xos = "xos";
        m_jsp = "jsp";
    }

    config::~config()
    {
        xos_stl::release_interface( m_pLastConfigProp );
        xos_stl::release_interface( m_pConfig );
        m_welcome_file_list.clear();
        m_pConfigProp = 0;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    config * config::get()
    {
        return config_ptr;
    }

    int config::init()
    {
        int ret = 0;

        if( 0 == ret )
        {
            ret = xos_stl::init_obj( config_ptr );
        }

        if( 0 == ret )
        {
            ret = config::get()->load( "xos_http.xml" );
        }

        return ret;
    }

    int config::term()
    {
        int ret = 0;
        ret = xos_stl::term_obj( config_ptr );
        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // proc

    int config::get_config()
    {
        int ret = 0;
        {
            xos_box::i_str_property * pProp = m_pConfigProp->str_prop( "app_path" );
            m_hot_replace_xos = pProp->str( "hot_replace_xos" );
            m_default_host = pProp->str( "default_host" );
            m_web_root = pProp->str( "web_root" );
            m_filter = pProp->str( "filter" );
            m_root = pProp->str( "root" );
            m_ssl = pProp->str( "ssl" );
        }
        {
            xos_box::i_str_property * pProp = m_pConfigProp->str_prop( "file_type" );
            m_xos = pProp->str( "xos" );
            m_jsp = pProp->str( "jsp" );
        }
        {
            xos_box::i_str_property * pProp = m_pConfigProp->str_prop( "params" );
            db_connect_str = pProp->str( "db_connect_str" );
            m_listener = pProp->str( "listener" );
            m_servlet = pProp->str( "servlet" );
            m_before = pProp->str( "before" );
            m_after = pProp->str( "after" );
        }
        {
            xos_box::i_list * pList = m_pConfigProp->list( "welcome" );
            xos_box::i_variant * pVT = 0;
            pList->reset();
            while( ( pVT = (xos_box::i_variant *)pList->next() ) )
            {
                const char * lpszFile = pVT->str();
                m_welcome_file_list.push_back( lpszFile );
            }
        }
        return ret;
    }

    int config::load( const char * lpszFile )
    {
        int ret = 0;

        if( 0 == ret )
        {
            char szFile[4096] = { 0 };
            tools::full_path( szFile, sizeof( szFile ), lpszFile );
            ret = mgr::container()->xml()->load_xml( szFile, 0, 0, &m_pConfig );
        }

        if( 0 == ret )
        {
            m_pConfigProp = m_pConfig->str_prop( 0 );
        }

        if( 0 == ret )
        {
            ret = get_config();
        }

        if( 0 == ret )
        {
            // m_pConfigProp所有权已经移交给xos_config模块
            // 所以不要释放它
            // 但是m_pLastConfigProp所有权归本模块
            m_pLastConfigProp = (xos_box::i_str_property *)mgr::container()->config()->set( "xos_http_config", m_pConfigProp, true );
            m_pConfig->set_str_prop( 0, false );
        }

        return ret;
    }

} // xos_http
